package com.github.pig.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.entity.SysTest;
import com.github.pig.admin.service.sys.SysTestService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 测试表 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-06-05
 */
@RestController
@RequestMapping("/sysTest")
public class SysTestController extends BaseController {
    @Autowired private SysTestService sysTestService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return SysTest
    */
    @GetMapping("/{id}")
    public R<SysTest> get(@PathVariable String id) {
        return new R<>(sysTestService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysTestService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  sysTest  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody SysTest sysTest) {
        return new R<>(sysTestService.insert(sysTest));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        SysTest sysTest = new SysTest();
        sysTest.setId(id);
        sysTest.setUpdateDate(new Date());
        sysTest.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(sysTestService.updateById(sysTest));
    }

    /**
     * 编辑
     * @param  sysTest  实体
     * @return success/false
     */
    @PutMapping
    public int edit(@RequestBody SysTest sysTest) {
        sysTest.setUpdateDate(new Date());
        return sysTestService.updates(sysTest);
    }
}
